//
//  TinyViewController.m
//  HappyNews
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "TinyViewController.h"

#import "HNRequestManager.h"

#import "RecreationViewModel.h"

#import "TinyTableViewCell.h"

#import <AVFoundation/AVFoundation.h>


#define TinyURL @"http://vcsp.ifeng.com/vcsp/appData/recommendGroupByTeamid.do?channelId=134915-0&showType=picture&useType=iPhone&positionId=&adapterNo=6.11.1&isNotModified=0"

@interface TinyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *TinyCellArr;

@property (nonatomic, strong) UITableView *TinyTabelView;


@property (nonatomic, strong) AVPlayer *player;

@end

@implementation TinyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    _TinyCellArr = [NSMutableArray array];
    
    [self setTinyUpData];
    
    [self addTinyTabelView];
    
}

- (void)setTinyUpData
{
    [HNRequestManager requestWithUrlString:TinyURL parDic:nil method:GET finish:^(NSData *data) {
        
        NSDictionary *TinyDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        // cell
        NSArray *bodyListTinyArr = TinyDic[@"bodyList"];
        
        for (NSMutableDictionary *TinyOneDic in bodyListTinyArr) {
            
            NSArray *videoListTinyArr = TinyOneDic[@"videoList"];
            
            for (NSMutableDictionary *TinyTwoDic in videoListTinyArr) {
                
                RecreationViewModel *model = [[RecreationViewModel alloc]init];
                
                [model setValuesForKeysWithDictionary:TinyTwoDic];
                
                [_TinyCellArr addObject:model];
            }
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [_TinyTabelView reloadData];
        });
        
    } error:^(NSError *error) {
        
    }];
    
    
}

- (void)addTinyTabelView
{
    _TinyTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 155) style:(UITableViewStylePlain)];
    
    _TinyTabelView.delegate = self;
    
    _TinyTabelView.dataSource = self;
    
    _TinyTabelView.rowHeight = kScreenHeight / 3.5;
    
    _TinyTabelView.backgroundColor = [UIColor whiteColor];
    
//    [_TinyTabelView registerClass:[TinyTableViewCell class] forCellReuseIdentifier:@"TinyTableViewCell"];
    
    [self.view addSubview:_TinyTabelView];

}

// 分行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _TinyCellArr.count;
}
// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TinyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TinyTableViewCell"];
    
    if (cell == nil) {
        cell = [[TinyTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"TinyTableViewCell"];
    }else{
        cell.imageF.hidden = NO;
        cell.imageQ.hidden = NO;
        [_player pause];
    }
    
    cell.model = _TinyCellArr[indexPath.row];
    
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecreationViewModel *model = _TinyCellArr[indexPath.row];
    
    TinyTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:model.mediaurl]];
    
    _player = [AVPlayer playerWithPlayerItem:item];
    
    
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
    
    layer.frame = CGRectMake(0, 0, cell.imageF.frame.size.width, cell.imageF.frame.size.height);
    
    cell.imageF.hidden = YES;
    cell.imageQ.hidden = YES;
    
    [cell.layer addSublayer: layer];
    
    [_player play];
    
    
    
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    for (id oneLayer in cell.layer.sublayers) {
        
        if ([oneLayer isKindOfClass:NSClassFromString(@"AVPlayerLayer")]) {
            
            [oneLayer removeFromSuperlayer];
            
        }
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
